<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class WebSiteConfig extends Model
{
    protected $table = 'web_site_configs';

    protected $fillable = [
        'label',
        'key',
        'value',
        'type',
        'remark',
        'group',
    ];

    public static function getValue($key, $default = null)
    {
        return cache()->rememberForever('web_site_config:'.$key, function () use ($key, $default) {
            return optional(self::where('key', $key)->value('value')) ?? $default;
        });
    }

    // 清除配置缓存
    public static function clearCache($key = null): void
    {
        if ($key) {
            Cache::forget('web_site_config:'.$key);
        } else {
            Cache::flush();
        }
    }
}
